import { nextTick } from './observe/next-tick';
import Watcher from './observe/watcher';
import { patch } from './vnode/patch';
export function mountComponent(el, vm) {
    vm.$el = el;
    // 即将挂载 
    callHook("beforeMount", vm);
    let updateComponent;
    updateComponent = function () {
        vm._update(vm._render());
    }
    new Watcher(vm, updateComponent, () => {
        callHook('beforeUpdate', vm);
    });
}


export function lifeCycleMixin(Vue) {
    Vue.prototype._render = function () {
        const vm = this;
        const options = vm.$options;
        const render = options.render;
        const vnode = render.call(vm);
        return vnode;
    }

    Vue.prototype._update = function (vnode) {
        const vm = this;
        let prevVnode = vm._vnode;
        vm._vnode = vnode;
        if (!prevVnode) {
            // 首次挂载
            vm.$el = patch(vm.$el, vnode);
        } else {
            // 更新 开始diff
            vm.$el = patch(prevVnode, vnode);
        }
        callHook("mounted", vm)
    }

    Vue.prototype.$nextTick = nextTick;
}

export function callHook(hook, vm) {
    const options = vm.$options;
    let hooks = options[hook];
    if (hooks) {
        for (let i = 0; i < hooks.length; i++) {
            hooks[i].call(vm);
        }
    }
}
